<!DOCTYPE html>
<html>
	<head lang="en">
		<meta charset="UTF-8">
		<title>login-AM</title>
		<script src="js/jquery-3.5.1.js" type="text/javascript" charset="utf-8"></script>
		<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css" />
		<script src="js/bootstrap.min.js" type="text/javascript" charset="utf-8"></script>
		<script src="js/jquery.cookie.js" type="text/javascript" charset="utf-8"></script>
		<style>

			.form{background: rgba(255,255,255,0.2);width:400px;margin:120px auto;}
        /*阴影*/
        .fa{display: inline-block;top: 27px;left: 6px;position: relative;color: #ccc;}
        input[type="text"],input[type="password"]{padding-left:26px;}
        .checkbox{padding-left:21px;}
    </style>
		<script>
			$(document).ready(function(){
            var rem = $.cookie('remember');
            if(rem){
                $("#remember").prop("checked",true);
                $("#username").val($.cookie("username"));
                $("#password").val($.cookie("password"));
            }
        });
    </script>
	</head>
	<body>
		<div class="container">
			<div class="form row">
				<div class="form-horizontal col-md-offset-3" id="login_form">
					<h3 class="form-title">LOGIN</h3>
					<div class="col-md-9">
						<div class="form-group">
							<i class="fa fa-user fa-lg"></i>
							<input class="form-control required" type="text" placeholder="Username" id="username" name="username" autofocus="autofocus"
							 maxlength="20" />
						</div>
						<div class="form-group">
							<i class="fa fa-lock fa-lg"></i>
							<input class="form-control required" type="password" placeholder="Password" id="password" name="password"
							 maxlength="8" />
						</div>
						<div class="form-group">
							<label class="checkbox">
								<input type="checkbox" id="remember" value="1" />记住我
							</label>

						</div>
						<div class="form-group col-md-offset-9">
							<a type="button" class="btn btn-primary pull-right" id="register" href="/register/">注册</a>
							<button type="button" class="btn btn-success pull-right" id="submit">登录</button>
						</div>
						<div class="form-group">
							<i class="fa fa-exclamation-triangle fa-lg" id="display_text"></i>
						</div>
					</div>
				</div>
			</div>
		</div>
		<script>
			$("#submit").click(function() {
				if ($("#username").val() != "" && $("#password").val() != "") {
					save_cookies();
					$.ajax({
						url: '/login_validation/',
						type: 'post',
						data: {
							'username': $("#username").val(),
							'password': $("#password").val()
						},
						dataType: 'json',
						success: function(args) {
							if (args.res == 1) {
								window.location = "/index/";
							} else {
								$("#display_text").html("用户名或密码错误.");
							}
						},
						error: function(data) {
							alert('ajax error');
						}
					});
				} else {
					alert("用户名或密码不能为空。");
				}
			});

			//保存到cookie
			function save_cookies() {
				if ($("#remember").prop("checked")) {
					var username = $("#username").val();
					var password = $("#password").val();

					$.cookie("remember", "true", {
						expires: 7
					});
					$.cookie("username", username, {
						expires: 7
					});
					$.cookie("password", password, {
						expires: 7
					});
				} else {
					$.cookie("remember", "false", {
						expires: -1
					});
					$.cookie("username", "", {
						expires: -1
					});
					$.cookie("password", "", {
						expires: -1
					});
				}
			};
		</script>
	</body>
</html>
